package com.ruoyi.system.mapper.waterMapper;

import com.ruoyi.system.domain.waterEnity.DistributionEntity;
import com.ruoyi.system.domain.waterEnity.ImageEntity;
import com.ruoyi.system.domain.waterEnity.ProductInfoBO;
import com.ruoyi.system.domain.waterEnity.SortEntity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface ProductMapper {
/**商品管理
 * 1.添加商品
 */
    @Insert("insert into water.product(product_name,sort_id,info) VALUES(#{product_name},#{sort_id},#{info})")
    int insertProduct(ProductInfoBO productInfoBO);
    @Insert("INSERT into water.norm(color,img,price,product_id,size,state,stock,name,info) VALUES(#{color},#{img},#{price},#{product_id},#{size},#{state},#{stock},#{name},#{info})")
    int insertNorm(ProductInfoBO productInfoBO);
    /**
     * 2.判断添加的商品名称是否已经存在
     */
    @Select("select * from water.product where product_name =#{product_name} ")
    String checkProduct_name(ProductInfoBO productInfoBO);

    /**
     * 3.添加分类
     */
    @Insert("insert into water.sort(parent_id,sort_name) VALUES(#{parent_id},#{sort_name})")
    int insertSort(ProductInfoBO productInfoBO);
    /**
     * 查看全部分类
     */
    @Select("select * from water.sort")
    List<SortEntity> selectSortList();
    /**
     * 4.查询全部商品
     */
    @Select("select * from water.product")

    List<ProductInfoBO> selectAllProduct();
    @Delete("<script>" +
            "delete from water.product where id in" +
            "<foreach collection='array' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int deleteProduct(Long [] ids);
    @Delete("<script>" +
            "delete from water.norm where product_id in" +
            "<foreach collection='array' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int deleteNormByProduct_id(Long [] ids);
    /**
     * 查询等于product_id等于id，主商品的子商品
     * @param id
     * @return
     */
    @Select("select n.id as 'child_id',product_id,img,size,color,price,stock,state,name,n.info,p.sort_id,p.product_name from water.product p left join water.norm  n on p.id=n.product_id where p.id = #{id}  ")
    List<ProductInfoBO> selectAllNorm(int id);

    /**
     * 根据id查询子商品
     *
     */
    @Select("select * from water.norm where id = #{id}")
    ProductInfoBO selectChildProductById(int id);


    /**
     *5.根据ID将商品上架，下架
     */
    @Update("update water.norm set state = #{state} where id = #{child_id}")
    int updateProductState(ProductInfoBO productInfoBO);
    /**
     * 修改主商品下全部商品状态
     */
    @Update("UPDATE water.product p left join water.norm n on p.id = n.product_id set n.state=#{productState},p.productState=#{productState} where p.id=#{id}")
    int updateProductState1(ProductInfoBO productInfoBO);
    /**
     * 检查子商品的状态
     */
    @Select("select * from water.norm where product_id = #{product_id} and state = 1")
    List<ProductInfoBO> checkChildProductState(ProductInfoBO productInfoBO);
    /**
     * 修改主商品状态
     */
    /**
     * 6.修改商品信息
     */
    @Update("update water.product set product_name = #{product_name},sort_id = #{sort_id} where id=#{id}")
    int updateProduct(ProductInfoBO productInfoBO);
    @Update("<script>update water.norm set " +
            "<if test = 'img != null'>img = #{img},</if>"+
            "<if test ='color != null'>color = #{color},</if>"+
            "<if test ='price != null'>price = #{price},</if>"+
            "<if test ='size != null'>size = #{size},</if>"+
            "<if test ='state != null'>state = #{state},</if>"+
            "<if test ='stock != null'>stock = #{stock},</if>"+
            "<if test ='product_id != null'>product_id = #{product_id},</if>"+
            "<if test ='name != null'>name = #{name},</if>"+
            "<if test ='info != null'>info = #{info} </if>"+
            "where id=#{child_id}</script>")
    int updateNorm(ProductInfoBO productInfoBO);

    /**
     * 检查主商品状态
     */
    @Select("select * from water.product where productState = 0 and id=#{product_id}")
    ProductInfoBO checkProductState(ProductInfoBO productInfoBO);
    /**
     * 7.根据多种条件查询商品
     *7.1.根据商品id查询
     */
    @Select("select * from water.product where id = #{id}")
    List<ProductInfoBO>  selectProductById(String id);
    /**
     * 7.2.根据商品名称查询
     */
    @Select("select * from water.product p  where p.product_name like concat('%', #{product_name}, '%')")
    List<ProductInfoBO> selectProductByProduct_name(String product_name);
    /**
     * 7.3.根据商品状态查询
     */
    @Select("select * from water.product p left join water.norm  n on p.id=n.product_id where n.state = #{state}")
    List<ProductInfoBO> selectProductByState(String state);
    @Select("select * from water.product p left join water.norm  n on p.id=n.product_id where p.sort_id = #{sort}")
    List<ProductInfoBO> selectProductBySort(String sort);

    /**配送信息管理
     * 1.送货员身份设置
     */
    @Update("update water.user set identify =#{identify} where id=#{id}")
    int updateUserByIdentify(String identify,int id);

    /**
     * 2.查看全部配送信息
     */
    @Select("select * from water.distribution")
    List<DistributionEntity> selectDistributionList();

    /**
     * 3.修改配送信息
     */
    @Update("update water.distribution set user_id =#{user_id},order_id=#{order_id},user_name=#{user_name},user_phone=#{user_phone},user_address=#{user_address},product_name=#{product_name},delivery_time=#{delivery_time},create_time=#{create_time},update_time=#{update_time}")
    int updateDistribution(DistributionEntity distributionEnity);

    @Update("update water.norm set stock=#{stock} where id=#{child_id}")
    int updateProductStock(ProductInfoBO productInfoBO);
    @Delete("<script>" +
            "delete from water.norm where id in" +
            "<foreach collection='array' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int deleteChildProduct(Long[] productids);
    @Select("select * from water.product where id = #{product_id}")
    List<ProductInfoBO> selectProductByProduct_id(String product_id);
    @Select("select * from water.sort where id=#{sort_id}")
    SortEntity selectSortBySort_id(int sort_id);
    //插入图片
    @Insert("insert into water.image(product_id,img,sequence) values(#{product_id},#{img},#{sequence})")
    int productImage(ImageEntity imageEntity);
}
